package com.voxel.sense.infomsg.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.com.common.app.BaseActivity;
import com.example.com.common.utils.L;
import com.example.com.common.utils.ListUtil;
import com.example.com.common.view.dialog.DialogCallBack;
import com.example.com.common.view.dialog.IDialog;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.view.PhotoView;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.constants.CommonConstants;

public class CommonImagePagerActivity extends BaseActivity {
    static final String EXTRA_longClickAble = "longClickAble";
    static final String EXTRA_uriList = "uriList";
    static final String EXTRA_position = "position";
    private ViewPager mPager;
    private ArrayList urlList;
    private int position;
    boolean longClickAble;
    TextView tv_position_desc;
    CommonImagePagerActivity context;
    String pageEnd;//页码后面的字符
    View.OnLongClickListener mOnLongClickListener;
    int mPosition;
    List<View> photoViews;//用来复用的PhotoView
    protected boolean enableFastClick;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_common_image_page;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        enableFastClick = true;
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setOffscreenPageLimit(2);
        tv_position_desc = (TextView) findViewById(R.id.tv_position_desc);
//        int icEmpty = R.mipmap.default_error;
//        options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).showImageForEmptyUri(icEmpty).showImageOnFail(icEmpty).showImageOnLoading(icEmpty).build();

        Intent intent = getIntent();
        urlList = (ArrayList) intent.getSerializableExtra(EXTRA_uriList);
        if (ListUtil.isEmpty(urlList)) {
            finish();
            return;
        }
        pageEnd = "/" + urlList.size();
        mPosition = position = intent.getIntExtra(EXTRA_position, 0);
        L.d("pageEnd:" + pageEnd + ",position:" + position);
        longClickAble = intent.getBooleanExtra(EXTRA_longClickAble, false);
        if (urlList.size() == 1) {
            tv_position_desc.setVisibility(View.GONE);
        }
        mPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return urlList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                PhotoView view;
                if (photoViews != null && photoViews.size() > 0) {
                    view = (PhotoView) photoViews.remove(0);
                } else {
                    view = new PhotoView(context);
                }
                view.setScaleType(ImageView.ScaleType.FIT_CENTER);
                view.enable();
                //view.setImageResource(imgsId[position]);
                view.setOnClickListener(mClickListener);

                if (longClickAble) {
                    if (mOnLongClickListener == null) {
                        mOnLongClickListener = new View.OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {
                                showPopuwindow();
                                return true;
                            }
                        };
                    }
                    view.setOnLongClickListener(mOnLongClickListener);
                }
                String imgPath = (String) urlList.get(position);
                String imgRealPath;
                if(imgPath.startsWith("http")){
                    imgRealPath = imgPath;
                }else{
                    imgRealPath = CommonConstants.Constance.FORMAL_FILE_URL_PRE+""+imgPath;
                }
                Glide.with(context)
                        .load(imgRealPath) // 加载路径
                        .apply(RequestOptions.fitCenterTransform().placeholder(R.mipmap.icon_img_loading)
                                .error(R.mipmap.default_error))
                        .into(view);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
                if (photoViews == null) {
                    photoViews = new ArrayList<View>();
                }
                photoViews.add((View) object);
            }
        });

        mPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                L.d("onPageSelected position:" + position);
                StringBuilder sb = new StringBuilder();
                sb.append(position + 1).append(pageEnd);
                tv_position_desc.setText(sb.toString());
                mPosition = position;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        mPager.setCurrentItem(position);
        //因为第一次没有onPageSelected的回调
        StringBuilder sb = new StringBuilder();
        sb.append(position + 1).append(pageEnd);
        tv_position_desc.setText(sb.toString());
    }


    OnClickListener mClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    };


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.no_anim, R.anim.scale_zoom_out);
    }

    public static void showImages(Context context, ArrayList<String> uriList, int position) {
        showImages(context, uriList, position, false);
    }

    public static void showImages(Context context, ArrayList<String> uriList, int position, boolean longClickAble) {
        Intent intent = new Intent(context, CommonImagePagerActivity.class);
        intent.putExtra(EXTRA_uriList, uriList);
        intent.putExtra(EXTRA_position, position);
        intent.putExtra(EXTRA_longClickAble, longClickAble);
        context.startActivity(intent);
        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(R.anim.scale_zoom_in, R.anim.no_anim);
        }
    }

    private void showPopuwindow() {
        String title = null;
        List<String> list = new ArrayList<String>();
        list.add("保存");

        IDialog.getInstance().showItemDialog(context, title, list, new DialogCallBack() {
            @Override
            public void onClick(int what) {
                switch (what) {
                    case 0:

                        break;
                }
            }
        });

    }

}
